package com.atguigu.common.validator.group;

import org.springframework.util.CollectionUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {

    private  Set<Integer> set =  new HashSet<>();
    //初始化方法，把vals中值set到集合中
    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        if(vals != null)
        for (Integer i : vals) {
            set.add(i);
        }
        ConstraintValidator.super.initialize(constraintAnnotation);
    }
    //校验传入的值，set是否包含
    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(integer);
    }
}
